<?php

namespace app\shop\controller\plus\european;

use app\common\model\european\EuropeanActivity;
use app\shop\controller\Controller;
use app\common\model\european\EuropeanChampion;
use app\common\model\european\EuropeanCountry;
use app\common\model\european\EuropeanMatch;
use app\common\model\european\EuropeanScore;
use app\common\model\european\EuropeanUserPicking;

use app\Request;

class European extends Controller
{


    /**
     *参赛国家列表
     * countrylist
     * \think\response\Json
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @Author kevin
     * @date 2024/6/3 17:24
     */

   public function index()
   {
       $params = $this -> postData();
       $map = [];
       $list = EuropeanCountry::where($map) -> order('european_country_id desc') -> paginate($params);
       return $this->renderSuccess('', compact('list'));

   }

    /**
     *新增
     * addcountry
     * \think\response\Json
     * @return \think\response\Json
     * @Author kevin
     * @date 2024/6/4 8:59
     */
   public function addcountry()
   {
       $params = $this -> postData();
       if(EuropeanCountry::createData($params)){
           return $this->renderSuccess('添加成功');
       }else{
           return $this->renderError('添加失败');
       }
   }

    public function deletecountry()
    {
        $params = $this -> postData();
        if(EuropeanCountry::where('european_country_id',$params['european_country_id']) -> delete()){
            return $this->renderSuccess('删除成功');
        }else{
            return $this->renderError('删除失败');
        }
    }


    /**
     *编辑
     * editcountry
     * @param $european_country_id
     * \think\response\Json
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @Author kevin
     * @date 2024/6/4 8:59
     */
    public function editcountry($european_country_id=0)
    {
        $params = $this -> postData();
     
        $model = EuropeanCountry::where('european_country_id',$european_country_id) -> find();
        if ($this->request->isGet()) {
            return $this->renderSuccess('', compact('model'));
        }
        // 修改记录
        if (EuropeanCountry::where('european_country_id',$european_country_id) -> update($params)) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }


    /**
     *新增
     * addscore
     * \think\response\Json
     * @return \think\response\Json
     * @Author kevin
     * @date 2024/6/5 14:44
     */
    public function addscore()
    {
        $params = $this -> postData();
        if(EuropeanScore::createData($params)){
            return $this->renderSuccess('添加成功');
        }else{
            return $this->renderError('添加失败');
        }
    }

    /**
     *删除
     * deletescore
     * \think\response\Json
     * @return \think\response\Json
     * @Author kevin
     * @date 2024/6/5 14:44
     */
    public function deletescore()
    {
        $params = $this -> postData();
        if(EuropeanScore::where('european_score_id',$params['european_score_id']) -> delete()){
            return $this->renderSuccess('删除成功');
        }else{
            return $this->renderError('删除失败');
        }
    }


    /**
     *删除赛事
     * deletescore
     * \think\response\Json
     * @return \think\response\Json
     * @Author kevin
     * @date 2024/6/6 16:23
     */
    public function deletematch()
    {
        $params = $this -> postData();
        if(EuropeanMatch::where('european_match_id',$params['european_match_id']) -> delete()){
            return $this->renderSuccess('删除成功');
        }else{
            return $this->renderError('删除失败');
        }
    }

    /**
     *编辑
     * editscore
     * @param $european_score_id
     * \think\response\Json
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @Author kevin
     * @date 2024/6/5 14:44
     */
    public function editscore($european_score_id=0)
    {
        $params = $this -> postData();

        $model = EuropeanScore::where('european_score_id',$european_score_id) -> find();
        if ($this->request->isGet()) {
            return $this->renderSuccess('', compact('model'));
        }
        // 修改记录
        if (EuropeanScore::where('european_score_id',$european_score_id) -> update($params)) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }


    /**
     *编辑赛事
     * editeuropeanmatch
     * @param $european_match_id
     * \think\response\Json
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @Author kevin
     * @date 2024/6/4 15:29
     */
    public function editeuropeanmatch($european_match_id=0)
    {
        $params = $this -> postData();
        $map = [];
        $list = EuropeanScore::where($map) -> select();
        $model = EuropeanMatch::where('european_match_id',$european_match_id) -> find();
        $model['start_time'] = date('Y-m-d H:i:s',$model['start_time']);
        $model['end_time'] = date('Y-m-d H:i:s',$model['end_time']);

        $country_list = EuropeanCountry::where($map) -> select();

        if ($this->request->isGet()) {
            return $this->renderSuccess('', compact('model','list','country_list'));
        }
        // 修改记录
        $params['start_time'] = strtotime($params['start_time']);
        $params['end_time'] = strtotime($params['end_time']);

        if($params['country_id_l'] == $params['country_id_r']){
            return $this->renderError('不能选择相同的国家');
        }

        $european_country = EuropeanCountry::column('country,national_flag','european_country_id');
        $country_l = $european_country[$params['country_id_l']]['country']??'';
        $country_r = $european_country[$params['country_id_r']]['country']??'';
        $team =$country_l.' VS '.$country_r;
        $params['team'] = $team;
        $params['national_flag_l'] = $european_country[$params['country_id_l']]['national_flag']??'';
        $params['national_flag_r'] = $european_country[$params['country_id_r']]['national_flag']??'';

        if (EuropeanMatch::where('european_match_id',$european_match_id) -> update($params)) {
            if($params['european_score_id']){
                $this ->  updateMathData($european_match_id,$params['european_score_id']);
            }

            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     *新增赛事
     * addeuropeanmatch
     * \think\response\Json
     * @return \think\response\Json
     * @Author kevin
     * @date 2024/6/5 12:03
     */
    public function addeuropeanmatch()
    {

        $map = [];
        $country_list = EuropeanCountry::where($map) -> select();

        if ($this->request->isGet()) {
            return $this->renderSuccess('', compact('country_list'));
        }else{
            $params = $this -> postData();
            // 修改记录
            $params['start_time'] = strtotime($params['start_time']);
            $params['end_time'] = strtotime($params['end_time']);

            if($params['country_id_l'] == $params['country_id_r']){
                return $this->renderError('不能选择相同的国家');
            }

            $european_country = EuropeanCountry::column('country,national_flag','european_country_id');
            $country_l = $european_country[$params['country_id_l']]['country']??'';
            $country_r = $european_country[$params['country_id_r']]['country']??'';
            $team =$country_l.' VS '.$country_r;
            $params['team'] = $team;
            $params['national_flag_l'] = $european_country[$params['country_id_l']]['national_flag']??'';
            $params['national_flag_r'] = $european_country[$params['country_id_r']]['national_flag']??'';

            if (EuropeanMatch::createData($params)) {
                return $this->renderSuccess('新增成功');
            }
            return $this->renderError('新增失败');
        }

    }

    /**
     *更新竞猜比分结果
     * updateMathData
     * @param $european_match_id
     * @param $result_european_score_id
     * void
     * @return void
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @Author kevin
     * @date 2024/6/5 10:00
     */
    public function updateMathData($european_match_id=0,$result_european_score_id=0)
    {
        $map =[];
        $map[] = ['european_match_id','=',$european_match_id];
        $list = EuropeanUserPicking::where($map) -> select();
        $european_user_picking_ids = [];//猜中的记录
        $have_not_ids = [];//未猜中记录
        foreach ($list as $key => $value){
            if($value['european_score_id'] == $result_european_score_id){ //猜中比分的
                $european_user_picking_ids[] = $value['european_user_picking_id'];
            }else{
                $have_not_ids[] =  $value['european_user_picking_id'];
            }
        }
        if($european_user_picking_ids){
            EuropeanUserPicking::where('european_user_picking_id','in',$european_user_picking_ids) -> update(['is_prize' => 1,'result_european_score_id'=>$result_european_score_id]);
        }

        if($have_not_ids){
            EuropeanUserPicking::where('european_user_picking_id','in',$have_not_ids) -> update(['is_prize' => 2,'result_european_score_id'=>$result_european_score_id]);
        }

        return true;

    }

    /**
     *比分列表
     * europeanscorelist
     * \think\response\Json
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @Author kevin
     * @date 2024/6/3 17:25
     */
    public function europeanscorelist()
    {
        $params = $this -> postData();
        $map = [];
        $list = EuropeanScore::where($map) -> order('european_score_id desc') ->  paginate($params);
        return $this->renderSuccess('', compact('list'));

    }



    /**
     *赛事列表
     * europeanmatchlist
     * \think\response\Json
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @Author kevin
     * @date 2024/6/3 17:28
     */
    public function europeanmatchlist()
    {
        $map = [];
        $params = $this -> postData();
        $list = EuropeanMatch::where($map) -> order('european_match_id desc') -> paginate($params);
        $score_list = EuropeanScore::column('score','european_score_id');
        foreach ($list as $key => $value){

            $list[$key]['start_time_text'] = date('Y-m-d H:i:s',$value['start_time']);
            $list[$key]['end_time_text'] = date('Y-m-d H:i:s',$value['end_time']);
            $list[$key]['score_text'] = $score_list[$value['european_score_id']]??'';
            if($value['start_time'] <= time() && $value['end_time'] > time()){
                $list[$key]['status'] = 1;
                $list[$key]['status_text'] = '进行中';

            }elseif($value['end_time'] < time()){
                $list[$key]['status'] = 2;
                $list[$key]['status_text'] = '已结束';
            }else{
                $list[$key]['status'] = 0;
                $list[$key]['status_text'] = '未开始';
            }
        }

        return $this->renderSuccess('', compact('list'));

    }



    /**
     *比分竞猜列表
     * guessingscorelist
     * \think\response\Json
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\DbException
     * @Author kevin
     * @date 2024/6/3 17:33
     */
    public function guessingscorelist()
    {

        $params = $this -> postData();
        $list = EuropeanUserPicking::getList($params);
        return $this->renderSuccess('竞猜比分列表', compact('list'));
    }


    /**
     *竞猜冠军列表
     * europeanchampionlist
     * \think\response\Json
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\DbException
     * @Author kevin
     * @date 2024/6/3 17:52
     */
    public function europeanchampionlist()
    {
        $map = [];
        $params = $this -> postData();

        $list = EuropeanChampion::where($map) -> paginate($params);
        $country = EuropeanCountry::column('country','european_country_id');

        foreach ($list as $key => $value){

            $list[$key]['country_text'] = $country[$value['european_country_id']]??'';
            $list[$key]['result_country_text'] = $country[$value['result_european_country_id']]??'待公布';
            $list[$key]['is_prize_text'] = '进行中';
            if($value['is_prize'] == 1){
                $list[$key]['is_prize_text'] = '已猜中';
            }elseif($value['is_prize'] == 2){
                $list[$key]['is_prize_text'] = '未猜中';
            }else{
                $list[$key]['is_prize_text'] = '进行中';
            }
            $list[$key]['is_give_text'] = '';
            if($value['is_prize'] == 1){
                if($value['is_give'] == 1){
                    $list[$key]['is_give_text'] = '已发放';
                }else{
                    $list[$key]['is_give_text'] = '待发放';
                }
            }

        }


        return $this->renderSuccess('竞猜冠军列表', compact('list'));
    }

    /**
     * 更新竞猜冠军数据
     * updateChampionData
     * @param $european_match_id
     * @param $result_european_score_id
     * true
     * @return true
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @Author kevin
     * @date 2024/6/5 10:18
     */
    public function updateChampionData($result_european_country_id=0)
    {
        $map =[];
        $list = EuropeanChampion::where($map) -> select();
        $european_champion_ids = [];//猜中的记录
        $have_not_ids = [];//未猜中记录
        foreach ($list as $key => $value){
            if($value['european_country_id'] == $result_european_country_id){ //猜中比分的
                $european_champion_ids[] = $value['european_country_id'];
            }else{
                $have_not_ids[] =  $value['european_country_id'];
            }
        }
        if($european_champion_ids){
            EuropeanChampion::where('european_country_id','in',$european_champion_ids) -> update(['is_prize' => 1,'result_european_country_id'=>$result_european_country_id]);
        }

        if($have_not_ids){
            EuropeanChampion::where('european_country_id','in',$have_not_ids) -> update(['is_prize' => 2,'result_european_country_id'=>$result_european_country_id]);
        }
        return true;
    }


    /**
     * 比分猜中发放优惠券
     * updateIsGive
     * @param $european_user_picking_id
     * \think\response\Json
     * @return \think\response\Json
     * @Author kevin
     * @date 2024/6/5 15:14
     */
    public function updateIsGive($european_user_picking_id = 0)
    {
        if(EuropeanUserPicking::where('european_user_picking_id',$european_user_picking_id) -> update(['is_give' => 1])){
            return $this->renderSuccess('操作成功');
        }else{
            return $this->renderError('操作失败');
        }
    }

    /**
     *猜中冠军发放优惠券
     * updateChampionIsGive
     * @param $european_champion_id
     * \think\response\Json
     * @return \think\response\Json
     * @Author kevin
     * @date 2024/6/5 15:15
     */
    public function updateChampionIsGive($european_champion_id = 0)
    {
        if(EuropeanChampion::where('european_champion_id',$european_champion_id) -> update(['is_give' => 1])){
            return $this->renderSuccess('操作成功');
        }else{
            return $this->renderError('操作失败');
        }
    }

    public function editactivity()
    {
        $params = $this -> postData();
        $map = [];
        $country_list = EuropeanCountry::where($map) -> select();

        $european_country_id = $params['european_country_id']??'';

        $model = EuropeanActivity::find();
        if($model){
            $model['start_time'] = $model['start_time']? date('Y-m-d H:i:s',$model['start_time']):'';
            $model['end_time'] = $model['end_time']?date('Y-m-d H:i:s',$model['end_time']):'';
        }


        if ($this->request->isGet()) {
            return $this->renderSuccess('', compact('model','country_list'));
        }
        // 修改记录
        $params['start_time'] = strtotime($params['start_time']);
        $params['end_time'] = strtotime($params['end_time']);

        if (EuropeanActivity::createData($params)) {
            if($european_country_id){
                $this -> updateChampionData($european_country_id);
            }
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }
}